<!-- 支付宝WAP支付账号配置 -->
<template>
    <div>
        <div class="table-page-search-wrapper">
            <a-form layout="inline">
                <a-row type="flex" justify="space-between">
                    <a-col :md="8">
                        <a-form-item label="支付宝appId">
                            <a-input v-model="appId" allow-clear placeholder="请输入支付宝appId" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8">
                    </a-col>
                    <a-col :md="8">
                        <a-form-item >
                            <a-button style="margin-right: 20px" type="primary" @click="getData()">查询</a-button>
                            <a-button icon="plus" @click="add()">新增</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>
        <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ pageSize: 10, total: total ,showSizeChanger: true,pageSize: size}" size="small">
            <div slot="onOff" slot-scope="onOff">
                <a-switch checked-children="开" un-checked-children="关" disabled :checked="onOff == 1 ? true : false" />
            </div>
            <template slot="operation" slot-scope="text, record">
                <div class="editable-row-operations">
                    <a-button type="link" @click="update(record)">修改</a-button>
                    <a-popconfirm
                        title="是否确认删除当前支付宝WAP支付账户?"
                        ok-text="Yes"
                        cancel-text="No"
                        @confirm="del(record)"
                    >
                        <a-button type="link" class="errorColor" >删除</a-button>
                    </a-popconfirm>
                </div>
            </template>
        </a-table>

        <a-modal
            title="添加、修改品牌支付宝WAP支付账户"
            :visible="visible"
            :confirm-loading="confirmLoading"
            @ok="handleOk"
            @cancel="visible = false"
            >
            <div>
                <a-form :label-col="{ span: 6 }" :wrapper-col="{ span: 17 }">
                    <a-form-item label="支付宝appId">
                        <a-input v-model="item.appId" placeholder="请输入支付宝appId" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="支付宝私钥">
                        <a-input v-model="item.appPrivateKey" :autosize="{ minRows: 2, maxRows: 6 }" type="textarea" placeholder="请输入支付宝私钥" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="支付宝公钥">
                        <a-input v-model="item.alipayPublicKey" :autosize="{ minRows: 2, maxRows: 6 }" type="textarea" placeholder="请输入支付宝公钥" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="描述">
                        <a-input v-model="item.description" :autosize="{ minRows: 2, maxRows: 6 }" type="textarea" placeholder="请输入描述内容" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="是否开启">
                        <a-radio-group v-model="item.onOff" >
                            <a-radio :value="0">关闭</a-radio>
                            <a-radio :value="1">开启</a-radio>
                        </a-radio-group>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
    </div>
</template>

<script>
import { columns } from './table';
import { queryBrandAliWAPPayAccount , updateBrandAliWAPPayAccount } from '@/api/brand';
export default {
    data () {
        return {
            brandId: this.global.brandId,
            isload: false,
            visible: false,
            grade: undefined,
            gradeList: [],
            dataList: [],
            total: 0,
            columns,
            appId: '',
            item: {
                id: '',
                appId: '',
                weight: '',
                isOn: '',
            },
            page: 1,
            size: 10,
            confirmLoading: false,
            drawerVisible: false,
        };
    },
    async created(){
        try {
            this.getData()
        } catch (error) {
            this.$notification['error']({
                message: '提示',
                description: '网路丢失，请稍后再试！',
                duration: 8,
            })
        }
    },
    methods: {
        getBrand(){
            let _this = this
            let page = 0
            const fn = function(resolve){
                const params = {
                    'size':99,
                    'page':page,
                }
                queryBrand(params)
                .then(res=>{
                    if(res.success){
                        if(page == 0){
                            _this.brandList = res.data.content
                        }else{
                            _this.brandList = [..._this.brandList,...res.data.content]
                        }
                        if((res.data.totalElements > (page + 1) * 99)){
                            page++;
                            fn(resolve)
                        }else{
                            resolve()
                        }
                    }else{
                        _this.$notification['error']({
                            message: '提示',
                            description: res.errMessage,
                            duration: 8,
                        })
                    }
                })
            }
            return new Promise((resolve,rej)=>{
                fn(resolve)
            })
        },
        getData(){
            let _this = this
            _this.isload = true
            const params = {
                'brandId':_this.brandId,
                'appId':_this.appId,
                'size':_this.size,
                'page':_this.page - 1,
            }
            queryBrandAliWAPPayAccount(params)
            .then(res=>{
                _this.isload = false
                if(res.success){
                    _this.dataList = res.data.content
                    _this.total = res.data.totalElements    
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        // 添加
        add(){
            if(!this.brandId){
                this.$message.warning('请选择品牌')
                return
            }
            this.item.id = ''
            this.item.brandId = this.brandId
            this.item.appId = ''
            this.item.appPrivateKey = ''
            this.item.alipayPublicKey = ''
            this.item.description = ''
            this.item.onOff = '1'
            this.visible = true
        },
        // 修改
        update(item){
            this.item = JSON.parse(JSON.stringify(item))
            this.visible = true
        },
        handleOk(){
            let _this = this
            _this.confirmLoading = true
            if(_this.item.appId == ''){
                _this.$message.warning('请输入支付宝appId')
                return
            }
            if(_this.item.appPrivateKey == ''){
                _this.$message.warning('请输入支付宝应用私钥')
                return
            }
            if(_this.item.alipayPublicKey == ''){
                _this.$message.warning('请输入支付宝公钥')
                return
            }
            const params = {
                'brandId':_this.item.brandId,
                'id':_this.item.id,
                'appId':_this.item.appId,
                'appPrivateKey':_this.item.appPrivateKey,
                'alipayPublicKey':_this.item.alipayPublicKey,
                'description':_this.item.description,
                'onOff': _this.item.onOff,
            }
            updateBrandAliWAPPayAccount(params)
            .then(res=>{
                _this.confirmLoading = false
                if(res.success){
                    _this.$notification['success']({
                        message: '提示',
                        description: _this.item.appId ? '修改成功' : '添加成功',
                        duration: 8,
                    })
                    _this.visible = false
                    _this.getData()
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        del(){
            let _this = this
            const params = {
                'brandId':_this.brandId,
                'id':_this.item.id,
                'appId':_this.item.appId,
                'isDelete':1
            }
            updateBrandAliWAPPayAccount(params)
            .then(res=>{
                if(res.success){
                    _this.isUpdate = false
                    _this.$notification['error']({
                        message: '提示',
                        description: '删除成功',
                        duration: 8,
                    })
                    _this.getData()
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
    }
}

</script>
<style scoped>

</style>